package cn.szzsi.platform.controller.admin;

import cn.szzsi.common.util.BackUtil;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.model.BusinessBalance;
import cn.szzsi.model.BusinessBalanceDetail;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.math.BigDecimal;
import java.util.List;

public class BusinessCashingManageController extends ConfigController {
    public void list(){
        render(CommonUtil.TEMPLATE_PATH + "/platform/businesscashingmanage/tocashing_manage_list.html");
    }
    public void ajaxlist(){
        List<BusinessBalanceDetail> businessBalanceDetailList=BusinessBalanceDetail.dao.find("SELECT m.realName, b.id,b.createDate,b.cashingStatus,b.bankType,b.addMoney,m.phone,cb.name,cb.cardCount,cb.cardNumber,cb.cardAddress,cb.aliPayAccount,cb.weChatAccount FROM sys_business_balance_detail b LEFT JOIN sys_member m on b.bid=m.id LEFT JOIN sys_cash_bank cb on m.id=cb.bid where b.type=0 order by b.createDate desc");
        renderJson(businessBalanceDetailList);
    }

    @Before(Tx.class)
    public void process(){
        String id=getPara("id");
        Integer type=getParaToInt("type");
        String reason=getPara("reason");
        BusinessBalanceDetail businessBalanceDetail=BusinessBalanceDetail.dao.findById(id);
        String bid=businessBalanceDetail.getBid();
        BusinessBalance businessBalance=BusinessBalance.dao.getByBusinessId(bid);
        if(businessBalanceDetail.getCashingStatus()!=1){
            renderJson(BackUtil.fail("状态错误"));
            return;
        }
        if(type==0){//拒绝
            //拒绝理由,资金回退
            businessBalance.setBalance(businessBalance.getBalance().add(businessBalanceDetail.getAddMoney().multiply(new BigDecimal(-1))));
            businessBalance.setFreezeMoney(businessBalance.getFreezeMoney().add(businessBalanceDetail.getAddMoney()));
            businessBalance.update();
            businessBalanceDetail.setCashingStatus(2);
            businessBalanceDetail.setReason(reason);
            businessBalanceDetail.update();
        }else{
            businessBalance.setFreezeMoney(businessBalance.getFreezeMoney().add(businessBalanceDetail.getAddMoney()));
            businessBalance.update();
            businessBalanceDetail.setCashingStatus(0);
            businessBalanceDetail.update();
        }
        renderJson(BackUtil.success("操作成功"));
    }

}
